<?php

namespace App\Http\Controllers\Main;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ScoreController extends Controller
{
    use ResponseData;
    public function generator(Request $request){
        $arr = [
            'ipAddress'=>$request->ip(),
            'requestId'=>time().rand(1111,9999),
            'reqTime'=>date("YmdHis",time()),
            'type'=>'Applet',
            'mobile'=>'15267357511',
            'outTokenId'=>'',//第三方用户唯一标识与积一家授权关系一旦建立不可修改
            'outType'=>'',//注册授权积一家模式  01：第三方定制授权页  02：积一家授权页
            'callbackUrl'=>'',//积一家注册授权成功回跳 移动账户绑定成功回跳
            'channelMark'=>'',//请联系积一家专业人员所求
            'hmac'=>'',//签名值 该字段不参与签名 只参与验签
        ];
        $appkey='123456';
        //签名步骤一：按字典序排序参数
        ksort($arr);

        $str = "";
        foreach ($arr as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $str .= $k . "=" . $v . "&";
            }
        }
        $str = trim($str, "&");

        //签名步骤二：在str后加入KEY
        $str = $str . "&key=".$appkey;

        //签名步骤三：MD5加密
        $str = md5($str);

        //签名步骤四：所有字符转为大写
        $r_sign = strtoupper($str);
        $arr['hmac'] = $r_sign;
        return $this->success($arr);
    }
    public function curl_post($url, $data = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}
